#include <thread>
#include "../../source/dispacher.hpp"
#include "../../client/Requestor.hpp"
#include "../../client/Rpc_call.hpp"

int main()
{
    auto requestor = std::make_shared<ns_Req::Requestor>();
    auto caller = std::make_shared<ns_rpc_call::RpcCaller>(requestor);

    //把requestor里的OnResponse绑进dispacher里的RPC_RSP对应的回调中
    //OnResponse会把结果Message塞进去
    auto rsp_cb = std::bind(&ns_Req::Requestor::OnResponse,requestor.get(),std::placeholders::_1,std::placeholders::_2);

    auto dispacher = std::make_shared<ns_dispacher::Dispacher>();
    dispacher->Registercb<RpcResponse>(ns_fields::Mtype::RPC_RES,rsp_cb);

    //Client模块的回调是绑定dispacher注册表里的回调方法为回调对象。
    auto cb = std::bind(&ns_dispacher::Dispacher::OnMessage,dispacher.get(),std::placeholders::_1,std::placeholders::_2);

    auto client = ns_net::ClientFactory::ClientCreate("127.0.0.1",8888);

    client->SetMessageCallback(cb);

    client->connect();

    //用户获取conn
    auto conn = client->connection();
    Json::Value paras,res;
    paras["num1"] = 10;
    paras["num2"] = 20;

    //通过caller给服务端发送请求
    //Requestor模块的OnResponse结果有了，异步/同步返回给caller，拿到body字段result
    bool ret = caller->Call(conn,"Add",paras,res);
    if(ret != false)
    {
        LOG(INFO,"result: %d",res.asInt());
    }   
    //std::this_thread::sleep_for(std::chrono::seconds(2));


    client->shutdown();

    return 0;
}